package com.c.ctgproject.ui.collect

import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.DestroyTypeAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityDestoryTypeBinding
import com.c.ctgproject.entity.guang.DestroyPhotoListEntity
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import org.greenrobot.eventbus.Subscribe

/**
 *作者:LiBW
 *创建日期: 2021/9/10
 *描述: 毁型拍照列表 页面
 */
class DestroyTypeActivity : BaseActivityV2(true) {
    private val mBinding by lazy { ActivityDestoryTypeBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var page = 1
    private var search: String = ""
    private var mList = ArrayList<DestroyPhotoListEntity.DataBean.ListBean>()
    private val mAdapter by lazy { DestroyTypeAdapter(this, mList) }

    @Subscribe
    fun event(type: PositionEvent) {
        if (type.position != -1) {
            mList.removeAt(type.position)
            mAdapter.notifyItemRemoved(type.position)
        }
    }

    override fun event() {
        getData()
        mBinding.mCarRV.adapter = mAdapter
        mBinding.mCarRV.layoutManager = LinearLayoutManager(this)


        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
    }

    //0可拍照列表 显示拍照按钮 1 不显示拍照按钮
//    private var destructivePhotostatus: Int? = null
    override fun click() {
        mBinding.mTabLayout.handlerSelectReturnText {
            when (it) {
//                "全部" -> {
//                    destructivePhotostatus = null
//                    page = 1
//                    getData()
//                }
                "未完成" -> {
                    searchMine = false
//                    destructivePhotostatus = 0
                    page = 1
                    getData()
                }
//                "已完成" -> {
//                    destructivePhotostatus = 1
//                    page = 1
//                    getData()
//                }
                "我的" -> {
                    searchMine = true
                    page = 1
                    getData()
                }
            }
        }

    }

    private var searchMine = false
    fun getData() {
        if (searchMine) {
            //流程类型 1-预录入 2-登记入库 3-过磅 4-初检 5-预处理 6-新建拆解 7-完成拆解 8-毁型拍照
//            ApiMethods.apiSubscribe(ApiMethods.retrofit.myCreateHandlerOrder(search,  page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
//            ApiMethods.apiSubscribe(ApiMethods.retrofit.myHandlerCar(search, 8, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.destroyPhotoListV2(search, 1, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("我的车辆列表:$it")
                if (page == 1) mList.clear()
                val data = getGson().fromJson(it, DestroyPhotoListEntity::class.java).data
                data?.list?.let { list ->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mCarRV.handlerPosition(page)
                }
            })

        } else {
//            ApiMethods.apiSubscribe(ApiMethods.retrofit.destroyPhotoList(search, destructivePhotostatus, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.destroyPhotoList(search, 0, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
//            ApiMethods.apiSubscribe(ApiMethods.retrofit.destroyPhotoListV2(search, 0, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("毁型拍照列表:$it")
                val data = getGson().fromJson(it, DestroyPhotoListEntity::class.java).data
                data?.list?.let { l ->
                    if (page == 1) mList.clear()
                    mList.addAll(l)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mCarRV.handlerPosition(page)
                }
            })
        }
    }
}